package threads;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.List;

import opennlp.tools.util.InvalidFormatException;

import nlp.PortuguesePOSTagger;

public class POS_Query implements Runnable{
	
	String SQL;
	PortuguesePOSTagger pos;
	Connection conn;
	ResultSet result;
	String threadname;
	final int batchSize = 1000;  
	
	public POS_Query(String SQL_, Connection conn_, String threadname_) throws InvalidFormatException, FileNotFoundException, IOException{
		this.SQL = SQL_;
		this.conn = conn_;
		this.threadname = threadname_;
		this.pos = new PortuguesePOSTagger();
	}
	
	//long startTime = System.currentTimeMillis();
	//long endTime = System.currentTimeMillis();
    //long elapsedTime = (endTime - startTime)/1000; //in seconds
    //System.out.println("Total time required to execute 1000 queries using PreparedStatement with JDBC batch insert is :" + elapsedTime);
	
    @Override
    public void run() {
    	Statement statement;
		try {
			statement = conn.createStatement();
			ResultSet resultSet = statement.executeQuery(SQL);
			int count = 0;
			
			/* news + blogs
			resultSet.setFetchSize(224604);			
			String SQL = "INSERT INTO content_pos_tags(id_content, title_pos_tags, body_pos_tags) VALUES (?,?,?)";
			*/
			
			//comments
			resultSet.setFetchSize(13423);			
			String SQL_INSERT = "INSERT INTO comment_pos_tags (id_comment, comment_tagged) VALUES (?,?)";
			
			PreparedStatement ps = this.conn.prepareStatement(SQL_INSERT);
			while (resultSet.next()) {
				/* news and blogs
				int id = resultSet.getInt(1);
				String title = resultSet.getString(5);
				String body = resultSet.getString(6);				
				List<String> titleSentencesTagged = null;
				List<String> bodySentencesTagged = null;
				String title_pos = null;
				String body_pos = null;
				
				if (title != null && !title.isEmpty()) {
					titleSentencesTagged = pos.tag(title);
					StringBuilder titleTagged = new StringBuilder();
					for (String s : titleSentencesTagged) titleTagged.append(s + '\n');
					title_pos = titleTagged.toString();
				}

				if (body != null && !body.isEmpty()) {
					bodySentencesTagged = pos.tag(body);
					StringBuilder bodyTagged = new StringBuilder();
					for (String s : bodySentencesTagged) bodyTagged.append(s + '\n');
					body_pos = bodyTagged.toString();
				}				
				ps.setInt(1,id);
				ps.setString(2, title_pos);
				ps.setString(3, body_pos);
				*/
				
				int id = resultSet.getInt(1);				
				String comment = resultSet.getString(2);
				List<String> comment_tagged = null;
				String comment_pos = null;
				
				if (!comment.isEmpty()) {
					comment_tagged = pos.tag(comment);
					StringBuilder bodyTagged = new StringBuilder();
					for (String s : comment_tagged) bodyTagged.append(s + '\n');
					comment_pos = bodyTagged.toString();
				}
				
				ps.setInt(1,id);
				ps.setString(2, comment_pos);
				
				ps.addBatch();
				if(++count % batchSize == 0) {
			        int[] updates = ps.executeBatch();
			        int z = 0;
			        for (int i : updates) {
						z += updates[i];
					}
			        //System.out.println(":: " + this.threadname + " tagged " + count + "/"+"224604" + " articles");
			        System.out.println(":: " + this.threadname + " tagged " + count + "/"+"13420" + " articles");
			    }
	        }
			ps.executeBatch(); // insert remaining records
			this.conn.commit();
			ps.close();
			System.out.println(this.threadname + " finished");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();		        
		}
    }
}
